<?php
// +----------------------------------------------------------------------
// | huicmf [ huicmf快速开发框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2024 https://xiaohuihui.cc All rights reserved.
// +----------------------------------------------------------------------
// | Author: 小灰灰 <762229008@qq.com>
// +----------------------------------------------------------------------
// | Info:  分词器- 提取关键词
// +----------------------------------------------------------------------

namespace libs;

class GetKeywords
{

    const SYS_BAIDU_QCNUM = 10;
    const SYS_KEYWORDAPI = 0;   //关键词提取接口：0=默认；1=百度；2=讯飞

    public static function getKeywores($title, $content = "", $dicWordMax = 5)
    {
        if (empty($title)) {
            return "";
        }
        $content = isset($content) && $content ? $content : $title;
        $rt      = self::getKeywordData($title, $content, $dicWordMax);

        return $rt;
    }

    /**
     * 提取关键词
     *
     * @param $title
     * @param $content
     *
     * @return void
     */
    public static function getKeywordData($title, $content, int $dicWordMax)
    {
        if ( ! $title) {
            return self::dr_return_data(0, '分词接口-没有获取标题');
        }
        if ( ! (int)self::SYS_BAIDU_QCNUM) {
            return self::dr_return_data(0, '分词接口-没有分词数量');
        }
        if (self::SYS_KEYWORDAPI == 1) {  // 百度分词
            // TODO: 调用百度接口
        } elseif (self::SYS_KEYWORDAPI == 2) {    // 讯飞分词
            // TODO: 调用讯飞接口
        } else {    // 本地分词
            $analysis = new \PhpAnalysis\Analysis();
            $result   = $analysis->run($title, $dicWordMax);
            if ( ! $result) {
                return self::dr_return_data(0, '本地接口-没有分析出关键词');
            } else {
                return self::dr_return_data(1, "分词成功", $result);
            }
        }

    }

    /**
     * 数据返回统一格式
     */
    static function dr_return_data($code, $msg = '', $data = array())
    {
        return array(
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        );
    }

}
